﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for StartUp.xaml
    /// </summary>
    public partial class StartUp : Window
    {
        int myresult=-1;

        public int myResult
        {
            get { return myresult; }
            set { myresult = value; }
        }
        string fileName;

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }
        public StartUp()
        {
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            InitializeComponent();
        }
        XmlDocument doc = new XmlDocument();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Load RecentProject
            RecentProjectListView.Items.Clear();
            if (System.IO.File.Exists( "./RecentProject.xml"))
            {
                doc.Load("./RecentProject.xml");
                for( int i=(doc.FirstChild.ChildNodes.Count-1);i>=0;i--)
                {
                    XmlNode n=doc.FirstChild.ChildNodes[i];
                    StackPanel stk = new StackPanel();
                    stk.Margin = new Thickness(4);
                    stk.Orientation = Orientation.Horizontal;
                    Label lblName = new Label();
                    lblName.FontWeight = FontWeights.Bold;
                    lblName.Content = n.InnerText.Split('\\')[n.InnerText.Split('\\').Count()-2];
                   
                    Label lblPath = new Label();
                    lblPath.Foreground = Brushes.DimGray;
                    lblPath.Content = "(" + n.InnerText + ")";
                    stk.Children.Add(lblName);
                    stk.Children.Add(lblPath);

                    ListViewItem l = new ListViewItem();
                    l.Tag = n;
                    l.MouseEnter += new MouseEventHandler(l_MouseEnter);
                    l.MouseLeave += new MouseEventHandler(l_MouseLeave);
                    l.Content = stk;
                    l.MouseDoubleClick += new MouseButtonEventHandler(l_MouseUp);
                    RecentProjectListView.Items.Add(l);
                }
            }
            //Add even item
            for (int i = 0; i < this.listBoxCommand.Items.Count; i++)
            {
                ListBoxItem item = this.listBoxCommand.Items[i] as ListBoxItem;
                item.MouseEnter += new MouseEventHandler(item_MouseEnter);
                item.MouseLeave += new MouseEventHandler(item_MouseLeave);
                item.Selected += new RoutedEventHandler(item_Selected);
            }
        }

        void l_MouseLeave(object sender, MouseEventArgs e)
        {
            ListViewItem item = sender as ListViewItem;
            item.Background = Brushes.White;
        }

        void l_MouseEnter(object sender, MouseEventArgs e)
        {
            ListViewItem item = sender as ListViewItem;
            item.Background = Brushes.LightCyan;
        }

        void l_MouseUp(object sender, MouseButtonEventArgs e)
        {
            myresult = 2;
            XmlNode node=(sender as ListBoxItem).Tag as XmlNode;
            fileName = node.InnerText;
            if(System.IO.File.Exists(fileName))
            {
            this.Close();
            }
            else
            {
                MessageBox.Show("This project nolonger existed or removed, please select others");
                doc.ChildNodes[0].RemoveChild(node);
                doc.Save("./RecentProject.xml");
                Window_Loaded(null,null);
            }
        }

        void item_Selected(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void item_MouseLeave(object sender, MouseEventArgs e)
        {
            ListBoxItem item = sender as ListBoxItem;
            item.Background = Brushes.White;
            StackPanel stackPanel= item.Content as StackPanel;
            Label lb= stackPanel.Children[1] as Label;
            lb.Foreground = Brushes.Black;
        }

        void item_MouseEnter(object sender, MouseEventArgs e)
        {
            ListBoxItem item = sender as ListBoxItem;
            item.Background = Brushes.LightCyan;
            StackPanel stackPanel = item.Content as StackPanel;
            Label lb = stackPanel.Children[1] as Label;
        }

        private void NewProject(object sender, RoutedEventArgs e)
        {
            myResult = 0;
        }

        private void OpenProject(object sender, RoutedEventArgs e)
        {
            myResult = 1;
        }
    }
}
